package system

import (
	"MoSkeleton/models/base"
	"gorm.io/gorm"
)

type SysRoleDept struct {
	RoleId uint64 `gorm:"column:role_id;type:BIGINT;NOT NULL"`
	DeptId uint64 `gorm:"column:dept_id;type:BIGINT;NOT NULL"`
}

func (b *SysRoleDept) TableName() string {
	return "sys_role_dept"
}

func (b *SysRoleDept) DeleteByRoleIds(tx *gorm.DB, roleIds *[]uint64) (bool, error) {
	return base.GetModelDB(tx).Where("role_id in (?)", *roleIds).Delete(b).RowsAffected > 0, nil
}

func (b *SysRoleDept) AddRoleDepts(tx *gorm.DB, roleId uint64, deptIds []uint64) (bool, error) {
	roleDepts := make([]*SysRoleDept, 0)
	for _, deptId := range deptIds {
		roleDepts = append(roleDepts, &SysRoleDept{
			RoleId: roleId,
			DeptId: deptId,
		})
	}
	return base.GetModelDB(tx).Model(b).Create(roleDepts).RowsAffected > 0, nil
}
